<?php

/**
 * Created by PhpStorm.
 * User: yh
 * Date: 2017/6/5
 * Time: 20:34
 */
class Address_model extends BS_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('zone_model');
    }


    //根据地址ID查询收货地址
    function getAddressByAid($aid = "")
    {
        $this->db->select('*');
        $this->db->from('address');
        $this->db->where('address_id', $aid);
        $query = $this->db->get();
//        echo $this->db->last_query();
//        die();
        if ($query->num_rows() == 0)
        {
            return array();
        }
        return $query->row_array();
    }


    //根据UID查询收货地址
    function getAddressByUid($uid = "")
    {
        $this->db->select('*');
        $this->db->from('address');
        $this->db->where('uid', $uid);
        $query = $this->db->get();
        if ($query->num_rows() == 0)
        {
            return array();
        }
        //echo $this->db->last_query();
        return $query->result_array();
    }


    //添加收货地址
    function addAddress($data = array())
    {
        $this->db->insert('address', $data);
        return $this->db->insert_id();
    }

    //更新地址
    function updateAddressById($address_id, $data)
    {
        $this->db->where('address_id', $address_id);
        $this->db->update('address', $data);
        return $this->db->affected_rows();
    }

    //删除地址
    function delAddressById($aid = null, $uid = null)
    {
        $this->db->where('address_id', $aid);
        $this->db->where('uid', $uid);
        $this->db->delete('address');
        return $this->db->affected_rows();
    }

    /**
     * 根据uid获取用户收货地址详细信息
     * @param  int $uid 用户id
     * @return array
     */
    function getAllAdrressByUid($uid)
    {
        $adrress = $this->getAddressByUid($uid);
        $this->load->model('Zone_model', 'zone_model');
        $this->load->model('User_model', 'user_model');
        for ($i = 0; $i < count($adrress); $i++)
        {
            //省
            $adrress[$i]['province_text'] = $this->zone_model->getZonesById($adrress[$i]['province'])['name'];
            //城市
            $adrress[$i]['city_text'] = $this->zone_model->getZonesById($adrress[$i]['city'])['name'];
            //地区
            $adrress[$i]['district_text'] = !empty($this->zone_model->getZonesById($adrress[$i]['district']))?$this->zone_model->getZonesById($adrress[$i]['district'])['name']:"";
            // 手机号码
            //收货人
            //详细地址
            //是否是默认
            $users = $this->user_model->getUserByUid($uid);
            // $adrress[$i]['adrress_id1'] = $users['address_id'];//默认地址ID
            $adrress[$i]['default'] = $adrress[$i]['address_id'] === $users['address_id'] ? true : false;
            //创建时间
            $adrress[$i]['create_time'] = date('Y-m-d H:i:s', $adrress[$i]['create_time']);
            $adrress[$i]['update_time'] = date('Y-m-d H:i:s', $adrress[$i]['update_time']);
        }
        $arr =array();
        foreach ($adrress as $k=>$v)
            $arr[$v['address_id']]= $v;
        return $arr;
    }
}